import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ApiUrl } from '../api/ApiUrl';
import { ApiService } from '../api/api.service';
import { CompanyVO } from '../vo/CompanyVO';
import { ToastController } from '@ionic/angular';
import { ConnectVO } from '../vo/ConnectVO';
import { AnswerVO } from '../vo/Answervo';
import { Router } from '@angular/router';
@Component({
  selector: 'app-connectdetail',
  templateUrl: './connectdetail.page.html',
  styleUrls: ['./connectdetail.page.scss'],
})
export class ConnectdetailPage implements OnInit {

  constructor(private router: Router, private toastController: ToastController,
    public api: ApiService, public activatedRoute: ActivatedRoute) { }
  iscall: string;
  isvisit: string;
  cmoney: string;
  csituation: string;
  iid: string;
  item: ConnectVO;
  answerVO: AnswerVO;
  ciscall: string;
  cisvisit: string;
  ngOnInit() {
    this.iid = this.activatedRoute.snapshot.paramMap.get('iid');
    this.getOneConnect();
  }
  getOneConnect() {
    this.api.getItem<ConnectVO>(ApiUrl.getOneConnect, { 'id': this.iid }).subscribe(response => {
      if (response === null) {
        console.log('没有数据返回');
        return;
      }
      this.item = response;
      this.iscall = this.item.iscall;
      if ( this.iscall + '' === '1') {
          this.ciscall = '是';
      } else {
        this.ciscall = '否';
      }
      this.isvisit = this.item.isvisit;
      if ( this.isvisit + '' === '1') {
        this.cisvisit = '是';
    } else {
      this.cisvisit = '否';
    }
      this.cmoney = this.item.cmoney;
      this.csituation = this.item.csituation;
    }, () => {

      console.log('出错了');
    });
  }

  submit() {
    if (this.csituation.length === 0) {
      this.presentToast('请填写沟通情况');
      return;
    }
    if (this.cmoney.length === 0) {
      this.presentToast('请填写合作意向金额');
      return;
    }
    this.api.getItem<AnswerVO>(ApiUrl.toSaveConnectDetail, {'iid': this.iid, 'iscall': this.iscall, 'isvisit': this.isvisit,
    'csituation': this.csituation, 'cmoney': this.cmoney
    }).subscribe(response => {

      this.answerVO = response;
      this.presentToast(this.answerVO.msg);
      this.router.navigate(['/myconnectcompany']);
    }, () => {
      console.log('出错了');
    });
  }
  async presentToast(msg) {
    const toast = await this.toastController.create({
      message: msg,
      duration: 1000
    });
    toast.present();
  }
}
